<?php
/**
*
* @package phpBB3
* @version $Id$
* @copyright (c) 2005 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
*/

/**
* @ignore
*/
 
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include_once($phpbb_root_path . 'common.' . $phpEx);
include_once($phpbb_root_path . 'includes/functions_display.' . $phpEx);
include_once($phpbb_root_path . 'sv_common.' . $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup('stockvirtual');

$money = $user->data['user_money'];
$user_id 	= $user->data['user_id'];

$corp_min 			= 10;
$max_news_for_corp	= 20;
$TAIEX_MARKET_ID 	= '0001';

$page_stime	= microtime(true);

// get corp list 
if ($user_id != ANONYMOUS)
{
	// get user interested corp or hot 
	$corp_array = get_user_interested_corp($user_id, 20);	
}
else
{
	$corp_array = array();
	$corp_array[] = $TAIEX_MARKET_ID;	
}


// only shows news after this time 
$sql	= 'SELECT * FROM phpbb_users WHERE user_id=' . $user_id . ' ';
$result = $db->sql_query_limit($sql, 1);	
if ($thisrow = $db->sql_fetchrow($result))
{		
	$news_after_this_time 	= $thisrow['user_lastvisit'];
}
else
{
	$news_after_this_time 	= get_taiwan_time() - 3 * 24 * 60 * 60;
}

if ($news_after_this_time > get_taiwan_time() - 3 * 24 * 60 * 60)
	$news_after_this_time = get_taiwan_time() - 3 * 24 * 60 * 60;


	
if (count($corp_array) < $corp_min)
{
	$hot_corp_stime	= microtime(true);
	
	// get hot corps 
	$time_1_month_ago = get_taiwan_time() - 300 * 24 * 60 * 60;
	$sql 	= "SELECT corp_id, SUM(share*price) FROM sv_transaction where request_time > $time_1_month_ago GROUP BY corp_id  order by sum(share*price) desc";
	$result = $db->sql_query_limit($sql,$corp_min - count($corp_array) + 1); 
	
	while ($row = $db->sql_fetchrow($result))
	{
		if (is_valid_corp_id($row['corp_id']) && get_corp_chinese_name($row['corp_id']) != '')
		{		
			$corp_array[] = $row['corp_id'];
		}
	}
	
	$hot_corp_etime	= microtime(true) - $hot_corp_stime;
	echo "<!-- Getting hot corps takes $hot_corp_etime seconds -->\n ";
}



$news_listed = array();

foreach($corp_array as $corp_id)
{
	$TAIEX_market 	= false;
	$show_news		= false;
	
	if ($corp_id == $TAIEX_MARKET_ID)
	{
		$TAIEX_market 	= true;
		$corp_name 		= $TAIEX_MARKET_ID;
	}
	else
		$corp_name = get_corp_chinese_name($corp_id) . ' (' . $corp_id . ')';		
		
		
	$news_corp_stime	= microtime(true);
	
	$i 		= 0;	
	
	$sql 	= "SELECT * FROM sv_news WHERE corp_id=" . $corp_id . " AND time > $news_after_this_time ORDER BY time DESC";
	$result = $db->sql_query_limit($sql, $max_news_for_corp);
	while ($row = $db->sql_fetchrow($result))
	{
		$news_id	= $row['news_id'];
		$corp_id	= $row['corp_id'];
		
		// check if the user have read this news or not.
		if ($user_id != ANONYMOUS)
		{
			$sql2 	= "SELECT * FROM sv_read_news_record WHERE user_id=$user_id AND news_id=$news_id";
			$result2 = $db->sql_query_limit($sql2, 1);
			if ($db->sql_fetchrow($result2))
				continue;		
		}
					
		// check if the news is already listed
		if (in_array ($news_id, $news_listed))
			continue;
		
		if ($show_news == false)
		{
			// print out title
			$template->assign_block_vars('corp_list', array(
				'CORP_NAME'							=> $corp_name,
				'TAIEX_MARKET'						=> $TAIEX_market,
				)
			);	
		}
		$show_news		= true;

		$title			= $row['title'];
		$link			= $row['link'];
		$snippet		= $row['snippet'];		
		$retrieve_time	= $row['time'];	
		
		$template->assign_block_vars('corp_list.news', array(
				'TITLE'							=> $title,
				'LINK'							=> $link,
				'SNIPPET'						=> $snippet,				
				'USER_ID'						=> $user_id,
				'NEWS_ID'						=> $news_id,
				'RELATED_CORP_ID'				=> $corp_id,
				'RETRIEVE_TIME'					=> date('n.d G:i', $retrieve_time),
			)
		);
		
		$news_listed[] = $news_id;
		++$i;
					
		$corp_count['$corp_id'] += 1;		
	}
	
	$news_corp_etime	= microtime(true) - $news_corp_stime;
	echo "<!-- News for corp $corp_name ($i) takes $news_corp_etime seconds -->\n ";
}


$page_etime	= microtime(true) - $page_stime;
echo "<!-- Loading this page takes $page_etime seconds -->\n ";

display_forums('', $config['load_moderators']);

// include nav links.
include_nav_links();

// Assign index specific vars
$template->assign_vars(array(

	'FORUM_IMG'				=> $user->img('forum_read', 'NO_NEW_POSTS'),
	'FORUM_NEW_IMG'			=> $user->img('forum_unread', 'NEW_POSTS'),
	'FORUM_LOCKED_IMG'		=> $user->img('forum_read_locked', 'NO_NEW_POSTS_LOCKED'),
	'FORUM_NEW_LOCKED_IMG'	=> $user->img('forum_unread_locked', 'NO_NEW_POSTS_LOCKED'),

	'S_LOGIN_ACTION'			=> append_sid("{$phpbb_root_path}ucp.$phpEx", 'mode=login'),
	
	
	'S_DISPLAY_BIRTHDAY_LIST'	=> ($config['load_birthdays']) ? true : false,
	'U_MONEY'					=> number_format($money),
		
	'U_MARK_FORUMS'		=> ($user->data['is_registered'] || $config['load_anon_lastread']) ? append_sid("{$phpbb_root_path}index.$phpEx", 'hash=' . generate_link_hash('global') . '&amp;mark=forums') : '',
	'U_MCP'				=> ($auth->acl_get('m_') || $auth->acl_getf_global('m_')) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=main&amp;mode=front', true, $user->session_id) : '',
	
	'RANKING_DATE'		=> get_date_string($date),
	
	)
);

// Output page
page_header($user->lang['SV_NEWS']);

$template->set_filenames(array(
	'body' => 'stock_news.html')
);

page_footer();

?>